﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using FangDuShi_Admin.DBUtility;

namespace FangDuShi_Admin.DAL
{
    /// <summary>
    /// 用户 数据类
    /// </summary>
    public partial class PrvUser
    {
        public PrvUser()
        { }
        /// <summary>
        /// 得到一个对象实体,根据用户登陆号、密码
        /// <creator>wangl</creator>
        /// <date>2012-06-01</date>
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="password"></param>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public Model.PrvUser GetModelByLoginIDAndPassword(string loginID, string password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT top 1 * from t_PrvUser ");
            strSql.Append(" WHERE FLogID=@FLogID AND Fpassword=@FPassword");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.VarChar,50),
                    new SqlParameter("@FPassword", SqlDbType.VarChar,50)};

            parameters[0].Value = loginID;
            parameters[1].Value = password;

            FangDuShi_Admin.Model.PrvUser model = new FangDuShi_Admin.Model.PrvUser();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FangDuShi_Admin.Model.PrvUser DataRowToModel(DataRow row)
        {
            FangDuShi_Admin.Model.PrvUser model = new FangDuShi_Admin.Model.PrvUser();
            if (row != null)
            {
                if (row["FLogID"] != null)
                {
                    model.FLogID = row["FLogID"].ToString();
                }
                if (row["FUserName"] != null)
                {
                    model.FUserName = row["FUserName"].ToString();
                }
                if (row["Fpassword"] != null)
                {
                    model.Fpassword = row["Fpassword"].ToString();
                }
                if (row["FLastLoginTime"] != null && row["FLastLoginTime"].ToString() != "")
                {
                    model.FLastLoginTime = DateTime.Parse(row["FLastLoginTime"].ToString());
                }
                if (row["FLastLoginIPAddr"] != null)
                {
                    model.FLastLoginIPAddr = row["FLastLoginIPAddr"].ToString();
                }
                if (row["FLoginCnt"] != null && row["FLoginCnt"].ToString() != "")
                {
                    model.FLoginCnt = decimal.Parse(row["FLoginCnt"].ToString());
                }
                if (row["FProhibitLogin"] != null)
                {
                    model.FProhibitLogin = row["FProhibitLogin"].ToString();
                }
                if (row["FForeverAvailable"] != null)
                {
                    model.FForeverAvailable = row["FForeverAvailable"].ToString();
                }
                if (row["FAvailableDate"] != null && row["FAvailableDate"].ToString() != "")
                {
                    model.FAvailableDate = DateTime.Parse(row["FAvailableDate"].ToString());
                }
            }
            return model;
        }

        public void EditPassword(string logid, string password)
        {
            string strSql = "update t_PrvUser set Fpassword=@Fpassword where FLogID=@FLogID";
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.VarChar,50),
					
                    new SqlParameter("@Fpassword", SqlDbType.VarChar,50)};

            parameters[0].Value = logid;
            parameters[1].Value = password;
         

            DbHelperSQL.ExecuteSql(strSql, parameters);

        }
        /// <summary>
        /// 更新用户登录信息
        /// <creator>wangl</creator>
        /// <date>2012-06-01</date>
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="lastLoginIpAddr"></param>
        /// <param name="loginType"></param>
        public void UpdateUserLoginMsg(string loginID, string lastLoginIpAddr)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("UPDATE t_PrvUser SET FLastLoginTime=GETDATE(),");
            strSql.Append("FLastLoginIPAddr=@FLast_Login_IP_Addr,");
            strSql.Append("FLoginCnt=FLoginCnt+1");
          
            strSql.Append(" WHERE FLogID=@FLogID");
            SqlParameter[] parameters = {
				    new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FLast_Login_IP_Addr", SqlDbType.NVarChar,15)
                 
                                        };
            parameters[0].Value = loginID;
            parameters[1].Value = lastLoginIpAddr;
      
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //更新系统信息最近登录相关信息
          

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTran(cmdList);
            }
        }
        /// <summary>
        /// 获取组织下的人员数量
        /// <creator>wangl</creator>
        /// <date>2012-06-11</date>
        /// </summary>
        /// <param name="orgNo"></param>
        /// <returns></returns>
        public int GetOrgPrvUserCount(string orgNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(*) FROM t_PrvUser");
            strSql.Append(" WHERE FOrganize_No=@FOrganize_No");
            SqlParameter[] parameters = {
					new SqlParameter("@FOrganize_No", SqlDbType.NVarChar,25)
                                           };
            parameters[0].Value = orgNo;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return int.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 根据组织编号获取用户列表
        /// <creator>wangl</creator>
        /// <date>2012-06-11</date>
        /// </summary>
        /// <param name="orgNo"></param>
        /// <returns></returns>
        public List<Model.PrvUser> GetPrvUserListByOrgNo(string orgNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FLogID,FName,FPolice_No,FShort_Mobile,FMobile,FProp,FProhibit_Login,FLogin_Cnt,FAvailable_Date,FForever_Available,FLast_Login_IP_Addr,FLast_Login_Time,FEmp_Status,FSeq,FSex,FDutyID,FDutyNo,FOrganize_No,FDutyName ");
            strSql.Append(" FROM t_PrvUser ");
            strSql.Append(" WHERE FOrganize_No=@FOrganize_No order by FOrganize_No,FDutyID");
            SqlParameter[] parameters = {
					new SqlParameter("@FOrganize_No", SqlDbType.NVarChar,25)
                                           };
            parameters[0].Value = orgNo;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获取所有用户列表
        /// <creator>wangl</creator>
        /// <date>2012-10-16</date>
        /// </summary>
        /// <returns></returns>
        public List<Model.PrvUser> GetPrvUserListAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT   FLogID ,
        FUserName ,
        Fpassword ,
        FLastLoginTime ,
        FLastLoginIPAddr ,
        FLoginCnt ,
        FProhibitLogin ,
        FForeverAvailable ,
        FAvailableDate FROM dbo.t_PrvUser ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获取所有用户列表
        /// <creator>wangl</creator>
        /// <date>2012-10-16</date>
        /// </summary>
        /// <returns></returns>
        public List<Model.PrvUser> GetModelList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FName,FLogID,FBrief_Name,FOrganize_Name,FIsSign,FOrganize_No");
            strSql.Append(" FROM v_UserOrganize ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" ORDER BY FOrganize_No");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return DataTableToListOther(ds.Tables[0]);
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <creator>wangl</creator>
        /// <date>2012-06-11</date>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="tableName"></param>
        /// <param name="fields"></param>
        /// <param name="strWhere"></param>
        /// <param name="fOrderFiled"></param>
        /// <param name="orderType">0 ASC</param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<Model.PrvUser> GetListByPage(int pageSize, int pageIndex, string strWhere, string fOrderFiled, int orderType, out int total)
        {
            if (fOrderFiled == "")
            {
                fOrderFiled = "FName";
            }

            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 50),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@orderType", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int)  
                    };

            parameters[0].Value = "t_PrvUser";
            parameters[1].Value = "*";
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Value = orderType;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[8].Value.ToString());
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 根据组织编号递归分页获取数据
        /// </summary>
        /// <creator>wangl</creator>
        /// <date>2012-06-11</date>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="tableName"></param>
        /// <param name="fields"></param>
        /// <param name="strWhere"></param>
        /// <param name="fOrderFiled"></param>
        /// <param name="orderType">0 ASC</param>
        /// <param name="total"></param>
        /// <returns></returns>

        /// <summary>
        /// 原密码是否正确
        /// <creator>wuxp</creator>
        /// <date>2012-06-27</date>
        /// </summary>
        /// <summary>
        public bool IsPasswordMatch(string flogID, string fPassword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_PrvUser");
            strSql.Append(" where FLogID=@FLogID");
            strSql.Append(" and FPassword=@FPassword");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.NVarChar,50),
                    new SqlParameter("@FPassword", SqlDbType.NVarChar,50)};
            parameters[0].Value = flogID;
            parameters[1].Value = fPassword;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// 是否存在该记录
        /// <creator>wangl</creator>
        /// <date>2012-06-14</date>
        /// </summary>
        public bool Exists(string loginID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_PrvUser");
            strSql.Append(" where FLogID=@FLogID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.NVarChar,50)};
            parameters[0].Value = loginID;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
      
       
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2012-06-14</date>
        /// </summary>
        public FangDuShi_Admin.Model.PrvUser GetModel(string FLogID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from t_PrvUser ");
            strSql.Append(" where FLogID=@FLogID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.NVarChar,50)};
            parameters[0].Value = FLogID;

            FangDuShi_Admin.Model.PrvUser model = new FangDuShi_Admin.Model.PrvUser();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

      
        /// <summary>
        /// 获取登录ID的姓名
        /// <creator>wangl</creator>
        /// <date>2012-07-02</date>
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public string GetLoginName(string loginID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FName from t_PrvUser ");
            strSql.Append(" WHERE FLogID=@FLogID");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30)};

            parameters[0].Value = loginID;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 获取手机号码的姓名
        /// </summary>
        /// <param name="Mobile"></param>
        /// <returns></returns>
        public string GetMobileName(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT top 1 FName from t_PrvUser ");
            strSql.Append(" WHERE FMobile=@FMobile or FShort_Mobile=@FMobile ");
            SqlParameter[] parameters = {
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40)};

            parameters[0].Value = mobile;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 获取登录ID的组织编码
        /// <creator>wangl</creator>
        /// <date>2012-07-02</date>
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public string GetLoginOrganizeNo(string loginID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FOrganize_No from t_PrvUser ");
            strSql.Append(" WHERE FLogID=@FLogID");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30)};

            parameters[0].Value = loginID;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 获取警员虚拟短号
        /// <creator>wuxp</creator>
        /// <date>2012-07-20</date>
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public string GetPoliceShortMobie(string logID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FShort_Mobile from t_PrvUser ");
            strSql.Append(" WHERE FLogID=@FLogID");
            SqlParameter[] parameters = {
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30)};

            parameters[0].Value = logID;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }

        #region 私有方法

        /// <summary>
        /// 获得数据列表
        /// </summary>
        private List<FangDuShi_Admin.Model.PrvUser> DataTableToList(DataTable dt)
        {
            List<FangDuShi_Admin.Model.PrvUser> modelList = new List<FangDuShi_Admin.Model.PrvUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                FangDuShi_Admin.Model.PrvUser model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new FangDuShi_Admin.Model.PrvUser();
                    model = DataRowToModel(dt.Rows[n]);
                  
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        private List<FangDuShi_Admin.Model.PrvUser> DataTableToListOther(DataTable dt)
        {
            List<FangDuShi_Admin.Model.PrvUser> modelList = new List<FangDuShi_Admin.Model.PrvUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                FangDuShi_Admin.Model.PrvUser model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new FangDuShi_Admin.Model.PrvUser();
                    model = DataRowToModel(dt.Rows[n]);
                  
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        #endregion
    }
}
